package com.sgb.kjwl.viewmodel.self.publish;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.LeaseMemberListEntity;
import com.sgb.kjwl.view.ui.activity.self.lease.LeaseMemberPriceListActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.MToast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.databinding.ObservableField;
import retrofit2.Call;

public class LeaseMemberPriceListModel extends BaseViewModel {
    public ObservableField<Map> map = new ObservableField<>(new HashMap());
    private final LeaseMemberPriceListActivity activity;
    private int page = 1;
    private final int size = 10;

    public LeaseMemberPriceListModel(LeaseMemberPriceListActivity activity) {
        this.activity = activity;
    }

    //获取租赁列表
    public void getPublishListModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        map.get().put("current", page);
        map.get().put("limit", size);
        map.get().put("haveVipPrice", 2);//新增的列表haveVipPrice：2，  会员价管理列表haveVipPrice：1
        APIManager.getInstance().getLeaseAPI().getVipPersonalLeaseImg(map.get())
                .enqueue(new ICallBack<BaseEntity<ListResponse<List<LeaseMemberListEntity>>>>() {
                    @Override
                    public void onSuccess(BaseEntity<ListResponse<List<LeaseMemberListEntity>>> data) {
                        if(data.getData() != null){
                            activity.showList(data.getData().getList(), isLoadMore);
                        }else{
                            activity.showList(new ArrayList<>(), isLoadMore);
                        }

                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse<List<LeaseMemberListEntity>>>> call, Throwable t) {
                        activity.showList(new ArrayList<>(), isLoadMore);
                    }
                });
    }

    //检查出租信息是否能报价
    public void checkVipPriceAdd(String module,String no) {
        APIManager.getInstance().getLeaseAPI().checkVipPriceAdd(module,no).enqueue(new ICallBack<BaseEntity>(true) {
            @Override
            public void onSuccess(BaseEntity data) {
                try {
                    org.json.JSONObject jsonObject = new JSONObject(data.getData().toString());
                    String msg =  jsonObject.getString("msg");
                    int flag =  jsonObject.getInt("flag");//0可以设置报价，1 不可以
                    if(flag == 0){
                        activity.showCheckVipPriceAdd(no,module);
                    }else {
                        MToast.showToast(activity, msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

}
